/*******************************************************************************
 * Copyright (c) 2011 TXT e-solutions SpA
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * This work was performed within the IoT_at_Work Project
 * and partially funded by the European Commission's
 * 7th Framework Programme under the research area ICT-2009.1.3
 * Internet of Things and enterprise environments.
 *
 *
 * Authors:
 *     Cristoforo Seccia (TXT e-solutions SpA)
 *
 * Contributors:
 *      Domenico Rotondi (TXT e-solutions SpA)
 *******************************************************************************/
package it.txt.access.capability.demo.server;

import it.txt.access.capability.factory.CapabilitySchemaFactoryException;
import it.txt.access.capability.factory.CapabilitySchemaFactory;
import it.txt.access.capability.demo.server.view.ServerGUIController;
import it.txt.access.capability.demo.schema.factory.CapabilityDemoFactoryException;
import it.txt.access.capability.demo.schema.request.factory.CapabilityDemoRequestFactory;
import it.txt.access.capability.demo.schema.response.factory.CapabilityDemoResponseFactory;
import it.txt.access.capability.demo.server.view.WaitFrameController;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

/**
 * -----------------------------------------------------------------------------
 * Graphical Application that allows clients to connect and responde with capabi
 * lity response containing the capability authorization result. The server olso
 * provide to validate both the request and the capability within used. *
 *
 * ******************************************
 *
 * @version 1.0
 * @author  http://www.txtgroup.com
 * @author  Cristoforo Seccia (cristoforo.seccia@txtgroup.com)
 * @contributor Domenico Rotondi (domenico.rotongi@txtgroup.com)
 *
 * -----------------------------------------------------------------------------
 */
public class ServerMainListener {

    private static final Logger LOGGER = Logger.getLogger(ServerMainListener.class.getName());


    public static void main(String[] args) {
        String schemasPath = null;
        if (args.length == 0) {
            System.err.println("Usage: Capability Schemas Path.");
            System.exit(1);
        } else {
            schemasPath = new File(args[0]).getAbsolutePath();
        }
        System.out.println("Schemas path: " + schemasPath);
        initializeLookAndFeels();
        WaitFrameController wfc = null;
        try {
            wfc = new WaitFrameController("Access Capability Demo Server");
            wfc.displayApplicationLoadingFrame();
            CapabilitySchemaFactory.getInstance(schemasPath);
            CapabilityDemoRequestFactory.getInstance(schemasPath);
            CapabilityDemoResponseFactory.getInstance(schemasPath);
            if (wfc.isApplicationLoadingFrameVisible()) {
                wfc.closeApplicationLoadingFrame();
            }
            ServerGUIController sguic= new ServerGUIController();
            sguic.createAndShowGui();
        } catch (CapabilitySchemaFactoryException ex) {
            if (wfc.isApplicationLoadingFrameVisible()) {
                wfc.closeApplicationLoadingFrame();
            }
            LOGGER.log(Level.SEVERE, null, ex);
        }
        catch (CapabilityDemoFactoryException ex) {
            if (wfc.isApplicationLoadingFrameVisible()) {
                wfc.closeApplicationLoadingFrame();
            }
            LOGGER.log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Installs the JGoodies Look & Feels, if available, in classpath.
     */
    protected static void initializeLookAndFeels() {
        // if in classpath thry to load JGoodies Plastic Look & Feel
        try {
            LookAndFeelInfo[] lnfs = UIManager.getInstalledLookAndFeels();
            boolean found = false;
            for (int i = 0; i < lnfs.length; i++) {
                if (lnfs[i].getName().equals("JGoodies Plastic 3D")) {
                    found = true;
                }
            }
            if (!found) {
                UIManager.installLookAndFeel("JGoodies Plastic 3D",
                        "com.jgoodies.looks.plastic.Plastic3DLookAndFeel");
            }
            /*UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");*/
            UIManager.setLookAndFeel("com.jgoodies.looks.plastic.Plastic3DLookAndFeel");
        } catch (Throwable t) {
            try {
                LOGGER.log(Level.INFO, "Error while setting L&F. Set the system default L&F.");
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
